//
//  RootTabBarViewController.m
//  TestDemo
//
//  Created by zhangwenqiao on 2018/12/4.
//  Copyright © 2018年 zhangwenqiao. All rights reserved.
//

#import "RootTabBarViewController.h"
#import "HomeViewController.h"
#import "MyViewController.h"
#import "StoreViewController.h"
#import "PainterViewController.h"
#import "MainHomeViewController.h"
#import "UIColor+HexM.h"


@interface RootTabBarViewController ()
{
    NSMutableArray *barArray;
}

@property (nonatomic) NSInteger indexFlag;
@end

@implementation RootTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initData];
    [self initSubController];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //    self.tabBar.hidden = NO;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    //    self.tabBar.hidden = YES;
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

- (void)initData {
    barArray = [NSMutableArray array];
    for (UIView *btn in self.tabBar.subviews) {
        if ([btn isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [barArray addObject:btn];
        }
    }
    NSLog(@"点击前--%@", barArray);
    self.indexFlag = 0;
}

- (void)initSubController {
    //控制器初始化
    HomeViewController *home = [[HomeViewController alloc] init];
    UINavigationController *homeNav = [[UINavigationController alloc] initWithRootViewController:home];
    //    home.title = @"首页";
    
    MainHomeViewController *mainHome = [[MainHomeViewController alloc] init];
    UINavigationController *mainNav = [[UINavigationController alloc] initWithRootViewController:mainHome];
    
    StoreViewController *store = [[StoreViewController alloc] init];
    UINavigationController *storeNav = [[UINavigationController alloc] initWithRootViewController:store];
    //    store.title = @"商城";
    
    PainterViewController *painter = [[PainterViewController alloc] init];
    UINavigationController *painterNav = [[UINavigationController alloc] initWithRootViewController:painter];
    //    painter.title = @"画师";
    
    MyViewController *my = [[MyViewController alloc] init];
    UINavigationController *myNav = [[UINavigationController alloc] initWithRootViewController:my];
    //    my.title = @"我的";
    
    self.viewControllers = @[homeNav, mainNav, painterNav, myNav];
    //    // 默认
    //    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    //    attrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    //    attrs[NSForegroundColorAttributeName] = [UIColor grayColor];
    //    // 选中
    //    NSMutableDictionary *attrSelected = [NSMutableDictionary dictionary];
    //    attrSelected[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    //    attrSelected[NSForegroundColorAttributeName] = [UIColor colorWithHexString:@"#B6D0C9"];
    //    // tab设置
    //    UITabBarItem *item = [UITabBarItem appearance];
    //    [item setTitleTextAttributes:attrs forState:UIControlStateNormal];
    //    [item setTitleTextAttributes:attrSelected forState:UIControlStateSelected];
    
    [[UITabBar appearance] setTranslucent:NO];
}



// 点击tabbarItem自动调用
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    NSInteger index = [self.tabBar.items indexOfObject:item];
    NSLog(@"%ld", index);
    if (index != self.indexFlag) {
        //        NSMutableArray *arry = [NSMutableArray array];
        //        for (UIView *btn in self.tabBar.subviews) {
        //            if ([btn isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
        //                [arry addObject:btn];
        //            }
        //        }
        //        NSLog(@"点击后--%@", arry);
        
//        [self animationWithIndex:index];
        self.indexFlag = index;
    }
    
    //    if (self.selectedIndex != _index) {
    ////        [self animationWithIndex:self.selectedIndex];
    //        _index = self.selectedIndex;
    //    }
    
    if (self.selectedIndex == 0) {
        NSLog(@"点击了第一个selected %ld", self.selectedIndex);
    }else if(self.selectedIndex == 1) {
        NSLog(@"点击了第二个selected %ld", self.selectedIndex);
    }else{
        NSLog(@"点击了第三个selected %ld", self.selectedIndex);
    }
    
    if([item.title isEqualToString:@"首页"])
    {
        NSLog(@"点击了--首页");
    }
    if([item.title isEqualToString:@"商城"])
    {
        NSLog(@"点击了--商城");
    }
    if ([item.title isEqualToString:@"我的"]) {
        NSLog(@"点击了--我的");
    }
}

- (void)animationWithIndex:(NSInteger) index {
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.duration = 0.2;       //执行时间
    animation.repeatCount = 1;      //执行次数
    animation.autoreverses = YES;    //完成动画后会回到执行动画之前的状态
    animation.fromValue = [NSNumber numberWithFloat:0.7];   //初始伸缩倍数
    animation.toValue = [NSNumber numberWithFloat:1.3];     //结束伸缩倍数
    [[barArray[index] layer] addAnimation:animation forKey:nil];
}


@end
